package com.arm.controller.api.admin.customer;

import com.arm.controller.BaseController;
import com.arm.def.Constant;
import com.arm.module.User;
import com.arm.module.UserAccount;
import com.arm.module.dto.Rechage;
import com.arm.service.UserAccountService;
import com.arm.service.UserService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Andrew  on 2017/2/26.
 */
@Api(description = "后台客户接口")

@RestController
@RequestMapping(value = "/api/admin/customer")
public class CustomerController extends BaseController {
    /*@Autowired
    RedisCache redisCache;*/
    @Autowired
    UserService userService;

    @Autowired
    UserAccountService userAccountService;


    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1",required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10",required = false, paramType = "query", dataType = "int")
    })
    @GetMapping(value = "/list")
    public ResultObj<PageInfo<User>> getUserList(Integer page, Integer pageSize) {
        ResultObj resultObj = new ResultObj();
        try {

            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            PageHelper.startPage(page, pageSize);

            List<User> userList = userService.getUserList(new HashMap<String, String>() {{
                put("userName", "");
                put("realName", "");
            }});

            PageInfo<User> pageinfo = new PageInfo<User>(userList);
            resultObj.setData(pageinfo);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj;

    }

    @ApiOperation(value = "获取用户", notes = "获取用户")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "tid", value = "用户Id", required = true, paramType = "path", dataType = "long")
    })
    @GetMapping(value = "/{tid}")
    public ResultObj<User> getUser(@PathVariable("tid") Long tid) {

        ResultObj<User> resultObj = new ResultObj();
        try {
            User user = userService.getUserById(tid);
            resultObj.setData(user);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj;
    }
    @ApiOperation(value = "获取用户", notes = "获取用户")

    @PostMapping(value = "/modifiy")
    public ResultObj<User> modifyUser(@RequestBody User user) {

        ResultObj<User> resultObj = new ResultObj();
        try {
            userService.modifyUser(user);

        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj;
    }



    @ApiOperation(value = "获取用户账户列表", notes = "获取用户列表")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "tid", value = "用户Id", required = true, paramType = "path", dataType = "long")
    })
    @GetMapping(value = "/userAccount/{userId}")
    public ResultObj<List<UserAccount>> getUserAccountList(@PathVariable("userId") Long userId) {
        ResultObj resultObj = new ResultObj();
        try {
logger.info("userAccount>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

            List<UserAccount> userAccountList = userAccountService.getUserAccountListByUserId(userId);

            resultObj.setData(userAccountList);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj;

    }
    @ApiOperation(value = "充值", notes = "获取用户列表")
    @PostMapping(value = "/userAccount/{userId}")
    public ResultObj addUserAccount(@PathVariable("userId")Long userId,@RequestBody Rechage rechage) {
        ResultObj resultObj = new ResultObj();
        try {


             userAccountService.modifyUserAccount(userId,UserAccount.ACCOUNT_TYPE_BALANCE,rechage.getBalance(),rechage.getRemark());


        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }

        return resultObj;

    }
}
